#pragma once

#include "Comm.hpp"
#include "LoopThread.hpp"


class LoopThreadPool
{
private:
public:
    LoopThreadPool(EventLoop* base_loop)
        : _next_loop_idx(0), 
          _thread_count(0), 
          _base_loop(base_loop)
    {} 

    void SetThreadCount(int count) { _thread_count = count; }

    // 创建所有的从属线程 
    void Create()
    {
        if (_thread_count > 0)
        {
            _threads.resize(_thread_count);
            _loops.resize(_thread_count);
            for (int i = 0; i < _thread_count; ++i)
            {
                _threads[i] = new LoopThread();
                _loops[i] = _threads[i]->GetLoop();
            }
        }
        return;
    }

    EventLoop* NextLoop()
    {
        if (_thread_count == 0) return _base_loop;
        _next_loop_idx = (_next_loop_idx + 1) % _thread_count;
        return _loops[_next_loop_idx];
    }

    ~LoopThreadPool()
    {}

private:
    int _next_loop_idx;
    int _thread_count;                   // 从属线程数量 
    EventLoop* _base_loop;               // 主 Reactor，运行在主线程 
    std::vector<LoopThread*> _threads;   // 保存所有的 LoopThread 对象 
    std::vector<EventLoop*> _loops;      // 从属线程数量大于 0 则从 _loops 中进行线程 EventLoop 分配 
};


